package edu.bupt.c2string;

import java.util.*;

public class T13Anagrams {
    public List<List<String>> groupAnagrams(String[] strs) {
        HashMap<String,List<String>> cache = new HashMap();
        for (int i=0;i<strs.length;i++){
            char[] chars = strs[i].toCharArray();
            Arrays.sort(chars);
            String s = new String(chars);
            List<String> strings = cache.get(s);
            if (null == strings){
                List<String> list = new LinkedList<String>();
                list.add(strs[i]);
                cache.put(s, list);
            }else{
                strings.add(strs[i]);
            }
        }

        LinkedList<List<String>> lists = new LinkedList();
        for(List<String> i : cache.values()){
            lists.add(i);
        }
        return  lists;
    }
}
